<!-- 查专业 -->
<template>
	<view class="major">
		<!-- 顶部图片说明 -->
		<image class="top-bg"
			src="https://aihelp.oss-cn-hangzhou.aliyuncs.com/public/base/2023/10/13/f8d198c21a794519843c44c860998ff4.png"
			mode="widthFix"></image>

		<view class="contents">
			<view class="searchs">
				<view class="search-box">
					<view class="input-box flex alignCenter">
						<view class="search-select flex alignCenter" @click="handlePopup(1)">
							<view class="font12" v-if="chooseArr[0].chooseData">{{ (chooseArr[0].chooseData.scrollTitle +
								(chooseArr[1].chooseData ? '-' + chooseArr[1].chooseData.scrollTitle : '') + (chooseArr[2].chooseData ?
									'-' + chooseArr[2].chooseData.scrollTitle : ''))
								|| '请选择专业' }} </view>
							<view v-else class="font12">请选择专业</view>
							<uni-icons type="bottom" size="14" color="#000" style="margin-left: 10rpx;"></uni-icons>
						</view>
					</view>
				</view>
				<!-- v-if="chooseArr[0].chooseData" -->
				<view class="other-box font14 flex alignCenter" v-if="chooseArr[0].chooseData">
					<view class="btn1 flex font12" @click="handleSummarize(0)">学科总括</view>
					<view class="btn2 flex font12" @click="handleSummarize(1)">一级学科总括</view>
					<view class="btn3 flex font12" @click="handleSummarize(2)">专业总括</view>
				</view>
			</view>
			<view class="lists">
				<view class="content-item" v-for="(item, index) of schoolList" :key="index">
					<view class="flex">
						<view class="classFy font12" :style="`background: ` + colorList[item.schoolClassify]">{{ item.schoolClassify
						}}
						</view>
						<!-- 左侧图片 -->
						<view class="left">
							<image :src="item.logo" mode="widthFix"></image>
							<view class="area-name font12 flex">
								<uni-icons class="erae-icon" type="location-filled" size="14" color="#797979"></uni-icons>
								<!-- <image src="@/static/image/icons/area.png"></image> -->
								{{ item.provinceName }}
							</view>
						</view>

						<!-- 右侧数据 -->
						<view class="right">
							<view class="title font12 hiddenOneline">
								<span class="font16 bold schoolName">{{ item.schoolName }}</span>
								{{ item.collegeName }}
							</view>
							<view class="normal-title font12 bold">{{ item.subjectName + '-' + item.subjectOneName }} <text
									style="margin-left: 10rpx;" v-if="item.subjectOneStar"> {{ item.subjectOneStar }}</text></view>
							<view class="normal-title font12 bold">{{ item.majorCode + '' + item.majorName }} <text
									style="margin-left: 10rpx;" v-if="item.majorStar"> {{ item.majorStar }}</text> </view>
							<view class="type flex flexWrap">
								<view class="name font12" v-for="(val, i) of item.schoolType.split(',')" :key="i">{{ val }}</view>
							</view>

						</view>
					</view>
					<view class="btns u-font-28 flex justify-around">
						<view class="flex align-center" @click="handleJump(1, item)">
							<u-icon size="28" class="u-m-r-10"
								name="https://aihelp.oss-cn-hangzhou.aliyuncs.com/public/base/2023/11/11/fcec3e172ffc43838ba60ae8fa67cba6.png"></u-icon>
								专业详情
						</view>
						<view class="flex align-center" @click="handleJump(2, item)">
							<u-icon size="28" class="u-m-r-10"
								name="https://aihelp.oss-cn-hangzhou.aliyuncs.com/public/base/2023/12/05/64acfa49074c404d966a41d71e09c667.png"></u-icon>
								大学详情
						</view>
						
						<view class="flex align-center" @click="handleLibrary(item)">
							<u-icon size="28" class="u-m-r-10"
								name="https://aihelp.oss-cn-hangzhou.aliyuncs.com/public/base/2023/11/11/87c15603db764305a3d982f62e1f5ada.png"></u-icon>
								加入对比库
						</view>
					</view>
				</view>

			</view>
		</view>

		<view class="no-data" v-if="schoolList.length <= 0">
			<image src="http://image.alhelp.net/uploads/20231104/8d4f88d27dfddc10b427fbe997780eaf.png" mode="widthFix">
			</image>
			<view v-if="!chooseArr[0].chooseData" class="font14 color999">选择专业以查询数据</view>
			<view v-else class="font14 color999">暂无数据</view>
		</view>
		<view v-else-if="schoolList.length >= total" class="font12 color999 noMore-data"> - 没有更多数据了 - </view>

		<!-- 学科分类 -->
		<view class="pickerLayer" v-if="showPicker">
			<view class="layerMain">
				<view class="layerTitle flex alignCenter">

					<!-- <view class="cancel font14 colorccc" @click="closeClick">取消</view> -->
					<view class="title">选择学科分类</view>
					<uni-icons class="layerClose" type="closeempty" size="20" @click="closeClick"></uni-icons>
					<!-- <view class="sure font14 colorMain" @click="handleSure">确定</view> -->
				</view>
				<scroll-view scroll-x="true">
					<view class="flex alignCenter font14 chooseBox">
						<view v-if="layerActiveChoose >= index"
							:class="layerActiveChoose == index ? 'commonChoose activeLayerStyle hiddenOneline' : 'commonChoose hiddenOneline'"
							v-for="(item, index) in chooseArr" :key="index" @click="clickLayerTitle(index)">
							{{ item.chooseData ? item.chooseData.scrollTitle : '请选择' }}
						</view>
					</view>
				</scroll-view>
				<!-- 请求展示的·数据 -->
				<view class="dataBox">
					<scroll-view scroll-y="true" class="font14 dataScroll">
						<view :class="chooseArr[layerActiveChoose].chooseData.id == item.id ? 'scrollItem colorMain' : 'scrollItem'"
							v-for="(item, index) in scrollData" :key="index" @click="clickScrollItem(item)">
							{{ item.scrollTitle }}
							<uni-icons v-if="chooseArr[layerActiveChoose].chooseData.id == item.id" class="activeIcon"
								type="checkmarkempty" size="16"></uni-icons>
						</view>
					</scroll-view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getProvince,
		getSubject,
		getMajorBySubject,
		addMyContrast
	} from "@/api/index.js"
	import {
		listByConditions
	} from "@/api/toolbox.js"
	export default {
		data() {
			return {
				showPicker: false,
				scrollData: [], //学科门类和一级学科
				layerActiveChoose: 0,
				searchType: 'major',
				isClose: false, //是否在弹出框选择了x
				chooseArr: [{
					chooseData: ''
				}, {
					chooseData: ''
				}, {
					chooseData: ''
				}],

				total: 0,
				searchParam: {
					pageNum: 1,
					pageSize: 10,
					subjectCode: "",
					subjectOneCode: "",
					majorCode: "",
					isKwkd: true,
				},
				direction: '',
				colorList: {
					'综合': 'rgba(250, 205, 145, 0.6)',
					'理工': 'rgba(2, 167, 240, 0.6)',
					'财经': 'rgba(194, 128, 255, 0.6)',
					'医药': 'rgba(236, 128, 141, 0.6)',
					'民族': 'rgba(25, 204, 165, 0.6)',
					'农林': 'rgba(168,186,4,0.6)',
					'师范': 'rgba(8,164,54,0.62)',
					'体育': 'rgba(24,47,198,0.62)',
					'文法': 'rgba(192,23,118,0.62)',
					'艺术': 'rgba(20,97,119,0.62)'
				},
				schoolList: [],
			}
		},
		watch: {
			showPicker(n, o) {
				// console.error("v=====",n,o)
				if (!n && !this.isClose) {
					this.searchParam.subjectCode = this.chooseArr[0].chooseData.subjectCode
					this.searchParam.subjectOneCode = this.chooseArr[1].chooseData.subjectCode
					this.searchParam.majorCode = this.chooseArr[2].chooseData.majorCode
					this.getData()
				}
			}
		},
		onShow() {
			// this.getData();
		},
		methods: {
			handleClick(item) {},
			// 获取学科门类
			getSubjectList(type) {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				let param = {
					pageNum: 1,
					pageSize: 99999,
					type: type,
					year: new Date().getFullYear(),
				}
				if (type > 0) {
					param.subjectCode = this.chooseArr[0].chooseData.subjectCode;
				}
				getSubject(param).then((res) => {
					uni.hideLoading()
					if (res.code == 100000) {
						console.log('学科门类', res.data.rows)
						res.data.rows.forEach((item) => {
							item.scrollTitle = `(${item.subjectCode})${item.title}`
						})
						this.scrollData = res.data.rows
					}
				}).catch((err) => {
					uni.hideLoading()
				})
			},
			// 根据学科门类获取专业
			getMajorFromSubject() {
				let param = {
					year: new Date().getFullYear(),
					subjectOneCode: this.chooseArr[1].chooseData.subjectCode
				}
				getMajorBySubject(param).then((res) => {
					if (res.code == 100000) {
						console.log('专业', res)
						res.data.forEach((item) => {
							item.scrollTitle = `(${item.majorCode})${item.title}`
						})
						this.scrollData = res.data;
						//当查询专业数据为空的时候，关闭弹窗，设置layerActiveChoose为0
						if (res.data.length <= 0) {
							this.layerActiveChoose = 1
							this.showPicker = false;
						}
					}
				})
			},
			// 蒙层的头部模块的点击事件
			clickLayerTitle(idx) {
				this.layerActiveChoose = idx;
				this.dealSearchDiffToApi('scrollTitle')
			},
			// scroll的选择点击事件
			clickScrollItem(item) {
				this.chooseArr[this.layerActiveChoose].chooseData = item
				this.chooseArr.forEach((val, index) => {
					if (index > this.layerActiveChoose) {
						val.chooseData = ''
					}
				})
				if (this.layerActiveChoose >= 2) {
					this.layerActiveChoose = 2
					this.showPicker = false;
					// this.getData();
					return
				}
				this.dealSearchDiffToApi('scrollItem')
				this.layerActiveChoose++
			},
			// 根据搜索条件选择调用相应的api的处理方法
			dealSearchDiffToApi(clickModule) {
				if (clickModule == 'scrollItem') {
					if (this.searchType == 'major') {
						if (this.layerActiveChoose == 0) {
							this.getSubjectList(1)
						} else if (this.layerActiveChoose == 1) {
							this.getMajorFromSubject()
						}
					}
					return
				} else if (clickModule == 'scrollTitle') {
					if (this.searchType == 'major') {
						if (this.layerActiveChoose == 0) {
							this.getSubjectList(0)
						} else if (this.layerActiveChoose == 1) {
							this.getSubjectList(1)
						} else if (this.layerActiveChoose == 2) {
							this.getMajorFromSubject()
						}
					}
				}
			},
			// 确认选择
			// handleSure(){
			// 	this.showPicker = false;
			// },

			// 关闭选择弹框
			closeClick() {
				this.showPicker = false;
			},

			/**
			 * @param {Object} type 1、专业选择弹窗
			 */
			handlePopup(type) {
				this.dealSearchDiffToApi('scrollTitle');
				this.showPicker = true;
			},

			getData() {
				uni.showLoading({
					title: '数据加载中'
				})
				listByConditions(this.searchParam).then((res) => {
					console.log(res);
					uni.hideLoading();
					if (res.code == 100000) {
						if (this.searchParam.pageNum == 1) {
							this.schoolList = res.data.rows;
							this.total = res.data.total;
							return
						}
						this.schoolList.push.apply(this.schoolList, res.data.rows);
					}

				}).catch(err => {
					uni.hideLoading();
				})
			},

			/**
			 * @param {Object} type 0、学科总括，1、一级学科总括，2、专业总括
			 */
			handleSummarize(type) {
				//总括
				let code = this.chooseArr[type].chooseData.subjectCode;
				let name = this.chooseArr[type].chooseData.title;
				if (type == 1) {
					if (!this.chooseArr[type].chooseData) {
						return uni.showToast({
							title: '选择到一级学科层级才可以查看一级学科总括哦~',
							icon: 'none',
							duration: 2000
						})
					}
				}
				if (type == 2) {
					if (!this.chooseArr[type].chooseData) {
						return uni.showToast({
							title: '选择到专业层级才可以查看专业总括哦~',
							icon: 'none',
							duration: 2000
						})
					}
					code = this.chooseArr[type].chooseData.majorCode;
					name = this.chooseArr[type].chooseData.majorName;
				}


				// let items = {
				// 	subjectCode: data.subjectCode,
				// 	majorCode: data.majorCode,
				// 	subjectOneCode: data.subjectOneCode,
				// 	pageNum: 1,
				// 	pageSize: 100,
				// 	schoolCode: data.schoolCode,
				// 	collegeCode: data.collegeCode,
				// }


				uni.navigateTo({
					url: `/pages/toolbox/chooseMajor/major/summarize/summarize?type=${type + 1}&code=${code}&names=${name}`
				})
			},

			/**
			 * 页面跳转
			 * @param {Object} type 1、专业详情，2、学校详情
			 * @param {Object} item 当前操作行数据
			 */
			handleJump(type, item) {
				if (type == 1) {
					let items = {
						"collegeCode": item.collegeCode, //"023"
						"majorCode": item.majorCode, //"010101"
						"schoolCode": item.schoolCode, //"10001"
					}
					//专业详情
					uni.navigateTo({
						url: `/pages/toolbox/newIndex/newIndex?items=${JSON.stringify(items)}`
					})
					return
				}
				if (type == 2) {
					//学校详情
					uni.navigateTo({
						url: `/pages/toolbox/chooseMajor/school/schoolDetail?schoolCode=${item.schoolCode}&title=${item.schoolName}`
					})
					return
				}
			},

			// 加入对比库
			handleLibrary(item) {
				addMyContrast({
					collegeCode: item.collegeCode,
					createTime: item.createTime,
					isDel: 0,
					majorCode: item.majorCode,
					schoolCode: item.schoolCode,
					updateTime: item.updateTime,
					year: item.year,
				}).then(res => {
					console.log(res);
					if (res.code == 100000) {
						uni.showToast({
							title: '加入对比库成功',
							icon: 'none'
						})
					}
				})
			},


			//底部刷新
			onReachBottom() {
				if (this.schoolList.length >= this.total) return;
				// if(!this.isNext) return;
				this.searchParam.pageNum++;
				this.getData();
			}
		},

	}
</script>

<style lang="scss" scoped>
	page {
		background: #f2f2f2;
	}

	.pickerLayer {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, .15);
		z-index: 2;

		.layerMain {
			position: absolute;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 1000rpx;
			padding: 30rpx 0;
			box-sizing: border-box;
			border-radius: 40rpx 40rpx 0 0;
			background: #fff;
		}

		.layerTitle {
			text-align: center;
			position: relative;
			justify-content: space-around;

			.layerClose {
				position: absolute;
				right: 30rpx;
				bottom: 0;
			}
		}

		scroll-view {
			margin: 30rpx 0 0;
		}

		.chooseBox {
			// margin: 30rpx 0 0;
			padding: 0 30rpx;
			box-sizing: border-box;
			border-bottom: 1px solid $bordercolor;
			height: 80rpx;
			line-height: 80rpx;
		}

		.commonChoose {
			padding: 0 40rpx;
			flex-shrink: 0;
		}

		.activeLayerStyle {
			border-bottom: 2px solid $colorMain;
		}

		.dataScroll {
			height: 800rpx;
			overflow-y: scroll;
		}

		.scrollItem {
			padding: 0 30rpx;
			height: 100rpx;
			line-height: 100rpx;
			box-sizing: border-box;
			position: relative;

		}

		.activeIcon {
			color: $colorMain !important;
			position: absolute;
			right: 30rpx;
			bottom: 0;
		}
	}

	.noMore-data {
		text-align: center;
		// color: #afafaf;
		padding-bottom: 20rpx;
	}

	.no-data {
		text-align: center;
		margin-top: 120rpx;

		image {
			width: 120rpx;
			height: 120rpx;
		}
	}

	.contents {
		.searchs {
			margin: -84rpx 30rpx 26rpx 30rpx;
			// height: 208rpx;
			background: #ffffff;
			box-shadow: 0 0 12rpx rgba(0, 0, 0, .2);
			border-radius: 20rpx;
			position: sticky;
			top: 0rpx;
			/* #ifdef H5 */
			top: 80rpx;
			/* #endif */
			z-index: 2;
			padding-bottom: 12rpx;


		}

		.other-box {
			padding-top: 8rpx;
			justify-content: center;

			.btn1,
			.btn2,
			.btn3 {
				background-color: rgba(247, 221, 94, 1);
				width: 194rpx;
				height: 72rpx;
				align-items: center;
				justify-content: center;
				margin-right: 20rpx;
				color: #333;
				border-radius: 50rpx;
			}

			.btn3 {
				margin-right: 0px;
			}
		}

		.search-box {
			padding-top: 30rpx;
		}

		.input-box {
			justify-content: center;
			border: 1px solid #F2F2F2;
			// border-radius: 50rpx;
			border-radius: 12rpx;
			margin: 0 30rpx;
			height: 66rpx;

			input {
				margin: 0 10rpx;
				width: 400rpx;
			}
		}
	}

	.content-item {
		background: #ffffff;
		margin: 0 30rpx 30rpx;
		border-radius: 30rpx;
		box-shadow: 0 0 12rpx rgba(0, 0, 0, .3);
		padding-bottom: 40rpx;

		.btns {
			margin-top: 28rpx;
			align-items: center;
			view{
				background-color: #F9F9F9;
				border-radius: 100rpx;
				color: #000;
				padding: 20rpx 30rpx;
			}
			.btn1,
			.btn2,
			.btn3 {
				// background-color: rgba(247, 221, 94, 1);
				width: 194rpx;
				height: 72rpx;
				align-items: center;
				justify-content: center;
				// margin-right: 20rpx;
				color: #333;
				// border-radius: 50rpx;
			}

			image {
				width: 32rpx;
				height: 32rpx;
				margin-right: 8rpx;
			}

			.btn3 {
				margin-right: 0px;
			}
		}
	}

	.left {
		margin: auto 0;

		image {
			width: 140rpx;
			height: 140rpx;
			border-radius: 50%;
			margin: 0 30rpx;
			display: block;
		}

		.area-name {
			// width: ;
			color: #7F7F7F;
			padding-top: 20rpx;
			align-items: center;
			justify-content: center;
			line-height: 1em;

			.erae-icon {
				// width: 30rpx;
				// height: 30rpx;
				margin: 0 2rpx
			}
		}
	}

	.right {
		// margin-left: 48rpx;
		line-height: 1em;

		.title {
			margin: 36rpx 10rpx 20rpx 0;
			font-style: normal;
			color: #333333;

			.schoolName {
				margin-right: 20rpx;
			}
		}

		.normal-title {
			margin: 10rpx 0 0;
			// line-height: 1em;
		}

		.type {
			margin-top: 12rpx;

			.name {
				width: auto;
				// min-width: 54rpx;
				height: 36rpx;
				line-height: 36rpx;
				color: #333;
				border: 1px solid rgba(247, 221, 94, 1);
				background: rgba(247, 221, 94, 0.1);
				border-radius: 30rpx;
				padding: 0 10rpx;
				margin: 12rpx 24rpx 0 0;
			}
		}
	}

	.classFy {
		// position: absolute;
		margin-bottom: 8rpx;
		width: 126rpx;
		height: 50rpx;
		line-height: 50rpx;
		// background: rgba(25, 204, 165, 0.6);
		border-radius: 30rpx;
		border-top-right-radius: 0px;
		border-bottom-left-radius: 0px;
		color: #FFFFFF;
		text-align: center;
		position: absolute;
		z-index: 1;
	}

	.top-bg {
		width: 100%;
		height: 280rpx;
		background: rgba(247, 221, 94, 0.8);
	}
</style>